
package controller.Actions;

import controller.Forms.UserForm;
import dao.UserDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class UpDateUserAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            User user = (User) request.getSession().getAttribute("User");

            if (user == null) {

                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {

                User u = new User();
                UserForm userForm = (UserForm) form;
                u.setName(userForm.getTxtName());
                u.setEmail(userForm.getTxtEmail());
//                u.setPassword(userForm.getTxtPassword());
                u.setLstStatus(userForm.getLstStatus());
                u.setId(userForm.getTxtId());

                UserDAO userDAO = new UserDAO();
                userDAO.update(u);
                userDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }

        } catch (Exception e) {

            System.out.println("CADASTRARUSUARIOCATION - " + Erro.erroMsg[Erro.DADOS_INVÁLIDOS] + e.getMessage());
            return mapping.findForward("erroAlias");
        }

    }

}
